﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace 实验四._22
{
    public partial class Form1 : Form
    {
        private Bitmap bmall;//原始位图
        private Bitmap[] bmfragments = new Bitmap[48];
        private int displayindex = 0;//起始帧索引
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            bmall = new Bitmap(Application.StartupPath + "\\threerings.bmp");
            //一位数组的处理方法
            for (int row = 0; row < 8; row++)
            {
                for (int col = 0; col < 6; col++)
                {
                    bmfragments[row * 6 + col] = bmall.Clone(
                        new Rectangle(col * 75, row * 75, 75, 75), bmall.PixelFormat);
                }
                this.timer1.Interval = 80;
                this.timer1.Start();
            }
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            // 动画逻辑
            displayindex = (++displayindex) % 48;
            if (displayindex == 47 && this.timer1.Interval >= 28)
            {
                this.timer1.Interval -= 10;
            }
            //刷新界面
            this.Invalidate();
        }

        private void Form1_Paint(object sender, PaintEventArgs e)
        {
            e.Graphics.DrawString("动画展示", new Font("微软雅黑", 32), new SolidBrush(Color.OrangeRed),
              new Point(300, 20));
        }
        protected override void OnPaint(PaintEventArgs e)//双缓冲技术
        {
            //预先在内存中绘制所有内容,再一次性绘制到前端
            Bitmap buff = new Bitmap(this.ClientRectangle.Width,
                this.ClientRectangle.Height);
            Graphics g = Graphics.FromImage(buff);
            //绘制到缓冲区（内存）
            g.DrawImage(bmfragments[displayindex], new Point(350, 150));
            //绘制到前端
            e.Graphics.DrawImage(buff, 0, 0);
            //释放内存
            g.Dispose();


            base.OnPaint(e);
        }
    }
}
